package com.hualan.sfzy.controller.psyc;


import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.QuestionReply;
import com.hualan.sfzy.service.psyc.QuestionReplyService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("QuestionReply")
public class QuestionReplyController {

    @Autowired
    private QuestionReplyService service;

    @PostMapping
    public Result add(@RequestBody QuestionReply entity) {
        int num = service.add(entity);
        return num > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") Integer id) {
        int num = service.remove(id);
        return num > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping
    public Result removeBatch(Integer[] ids) {
        int num = service.removeBatch(ids);
        return num > 0 ? Result.success() : Result.error();
    }

    @PutMapping
    public Result edit(@RequestBody QuestionReply entity) {
        int num = service.edit(entity);
        return num > 0 ? Result.success() : Result.error();
    }

    @GetMapping
    public Result find() {
        List<QuestionReply> list = service.find();
        return Result.success(list);
    }

    @GetMapping("/page")
    public Result findPage(
            @RequestParam(value = CommonConstant.PAGE_NUM, required = false,
                    defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
            @RequestParam(value = CommonConstant.PAGE_SIZE, required = false,
                    defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize, QuestionReply entity) {
        Map<String, Object> map = service.findPage(pageNum, pageSize,
                entity);
        return Result.success(map);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        QuestionReply entity = service.findById(id);
        return Result.success(entity);
    }

    @GetMapping("/selectCountByQuestionId/{questionId}")
    public Result selectCountByQuestionId(@PathVariable("questionId") Integer questionId) {
        Integer count = service.selectCountByQuestionId(questionId);
        return Result.success(count);
    }
}
